import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:flutter_account_book/utils/db/db_helper.dart';
import 'package:get/get.dart';

import 'state.dart';

class RecordTypeLogic extends GetxController {
  RecordTypeState state = RecordTypeState();
  late int isIncome;
  @override
  void onInit() {
    super.onInit();
    _loadData();
  }

  _loadData() async {
    isIncome = (Get.arguments["isIncome"] as num).toInt();
    state.list.value = await DbHelper.helper.recordTypeDao.flowAll(isIncome);
  }

  reload() async {
    state.list.value = await DbHelper.helper.recordTypeDao.flowAll(isIncome);
  }

  Future<List<String>> getAllIcons() async {
    if (state.allIcons.isEmpty) {
      var jsonStr = await rootBundle.loadString("assets/data/allIconData.json");
      List<dynamic> list = jsonDecode(jsonStr);
      state.allIcons.value = list.map((item) => item.toString()).toList();
    }
    return state.allIcons;
  }
}
